package com.tps.service.impl;

import com.codingapi.tx.annotation.TxTransaction;
import com.tps.dao.OrderDao;
import com.tps.entity.Order;
import com.tps.feign.GoodsFeign;
import com.tps.service.OrderService;
import org.hibernate.service.spi.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {

    private final OrderDao orderDao;

    private final GoodsFeign goodsFeign;

    public OrderServiceImpl(OrderDao orderDao, GoodsFeign goodsFeign) {
        this.orderDao = orderDao;
        this.goodsFeign = goodsFeign;
    }

    @Override
    @TxTransaction(isStart = true)
    @Transactional
    public void save(Boolean isRollBack) {

        goodsFeign.save();

        Order order = new Order();
        order.setName("sss");
        orderDao.saveAndFlush(order);

        if (isRollBack) {
            throw new ServiceException("测试回滚");
        }
    }
}
